//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.standard.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.live.app.service.CacheService;
import com.ruoyi.live.base.domain.*;
import com.ruoyi.live.base.mapper.LiveExamObjectiveScoreDetailMapper;
import com.ruoyi.live.base.mapper.LiveProductSourceMapper;
import com.ruoyi.live.base.mapper.LiveTaskUserChannelMapper;
import com.ruoyi.live.base.mapper.LiveTaskUserDetailMapper;
import com.ruoyi.live.standard.service.CountResult;
import com.ruoyi.live.standard.service.StandardService;
import com.ruoyi.live.tool.commons.IConvert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;

@Service
public class SevenStandardImpl implements StandardService {
    private static final Logger log = LoggerFactory.getLogger(SevenStandardImpl.class);
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    LiveProductSourceMapper sourceDao;
    @Autowired
    LiveTaskUserDetailMapper detailDao;
    @Autowired
    CacheService cacheService;
    @Autowired
    LiveExamObjectiveScoreDetailMapper scoreDetailDao;
    @Autowired
    LiveTaskUserChannelMapper channelDao;

    public SevenStandardImpl() {
    }

    public String getStandardCode() {
        return "SCORE_RULE_PRODUCT_SEVEN";
    }

    public CountResult doCountScore(LiveExamUser examUser, Long scoreId) {
        LiveScoreStandard standard = this.cacheService.getStandardByCode(this.getStandardCode());
        List<LiveTaskUser> tasks = examUser.getTaskUserList();
        Iterator var5 = tasks.iterator();

        while(var5.hasNext()) {
            LiveTaskUser task = (LiveTaskUser)var5.next();
            if (task.getTaskType() == 2) {
                LiveExamObjectiveScoreDetail scoreDetail = (new LiveExamObjectiveScoreDetail()).setTaskUserId(task.getId()).setScoreId(scoreId).setState(1).setProductId(task.getProductId());

                try {
                    if (standard == null) {
                        scoreDetail.setState(2).setErrDesc("获取评分标准失败,code为SCORE_RULE_PRODUCT_SEVEN");
                        this.scoreDetailDao.insert(scoreDetail);
                        break;
                    }

                    scoreDetail.setStandardId(standard.getId()).setStandardName(standard.getStandardName());
                    List<LiveTaskUserChannel> channels = this.channelDao.selectList(new QueryWrapper((new LiveTaskUserChannel()).setTaskUserId(task.getId()).setChannelType(2)));
                    if (channels.size() > 0) {
                        if (((LiveTaskUserChannel)channels.get(0)).getEndTime() != null) {
                            Long time = (((LiveTaskUserChannel)channels.get(0)).getEndTime() - ((LiveTaskUserChannel)channels.get(0)).getCreateTime()) / 1000L;
                            Long minute = (time - time % 60L) / 60L;
                            Boolean hasMore = minute.intValue() >= IConvert.object2int(this.cacheService.getParam("SYSTEM_LIVE_SCORE_MINUTE"));
                            if (!hasMore && ((LiveTaskUserChannel)channels.get(0)).getScreenVideoLength() != null && ((LiveTaskUserChannel)channels.get(0)).getScreenVideoLength() >= IConvert.object2int(this.cacheService.getParam("SYSTEM_LIVE_SCORE_MINUTE")) * 60) {
                                hasMore = true;
                            }

                            if (hasMore) {
                                scoreDetail.setHasScore(1).setState(1).setScore(standard.getStandardValue());
                            } else {
                                scoreDetail.setHasScore(1).setState(1).setScore(new BigDecimal(0));
                            }
                        } else {
                            scoreDetail.setHasScore(1).setState(1).setScore(new BigDecimal(0));
                        }
                    } else {
                        scoreDetail.setHasScore(2).setState(2).setErrDesc("获取直播频道失败,SCORE_RULE_PRODUCT_SEVEN").setScore(new BigDecimal(0));
                    }

                    this.scoreDetailDao.insert(scoreDetail);
                } catch (Exception var12) {
                    var12.printStackTrace();
                    log.error("LiveExamUser的id为{}计算得分出错，SCORE_RULE_PRODUCT_SEVEN", examUser.getId(), var12);
                    scoreDetail.setState(2).setErrDesc("计算得分出错，SCORE_RULE_PRODUCT_SEVEN");
                    this.scoreDetailDao.insert(scoreDetail);
                }
            }
        }

        return new CountResult(0.0D);
    }
}
